extends Control

@onready var main = get_node("/root/Main")

var deathmessages = [
"Purple bubbles are bouncy. They deflect debris.",
"Green bubbles are sticky. They stick.",
"Red bubbles explode. Only on the inside, though.",
"Shoot a load of tiny bubbles to move. It’s like a jet-pack!",
"Don’t go too fast. You might hit something.",
"Sometimes debris is fast. Nothing you can really do about that.",
"Bubbles can block debris. Spew them if you’re in trouble.",
"Catch aliens whenever you can. They love it, I promise.",
"Big bubbles are hard to pop. Don’t worry if debris hits them.",
"If you make a bubble too big, it bursts and you go flying.",
"Bigger bubbles catch bigger objects.",
"People used to laugh at Bubbleboy. Look at them now.",
"How does Bubbleboy exhale with no mouth? Don’t ask silly questions.",
"Red aliens are 70% nitroglycerin. It explodes.",
"Green aliens are 99% silly.",
"Purple aliens are 60% brain.",
"Rocks are bad. Don’t touch them.",
"…wait, how can you hear in space?"
]

func _ready():
	var y = FileAccess.open("user://save_game.dat", FileAccess.READ)
	if y != null:
		$mainmenu/Label.text = "High Score: " + str(y.get_32())
		y.close()

func die():
	var tween = get_tree().create_tween()
	var y = FileAccess.open("user://save_game.dat", FileAccess.READ)
	if y != null:
		if y.get_32() < main.score:
			$Label3.visible = true
			tween.tween_property($Label3, "modulate:a", 1, 1)
			y = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
			y.store_32(main.score)
		y.close()
	else:
		y = FileAccess.open("user://save_game.dat", FileAccess.WRITE)
		y.store_32(main.score)
		y.close()
	$Label2.visible = true
	$Button.visible = true
	$Button5.visible = true
	$Button6.visible = true
	$Label5.visible = true
	$Label5.text = deathmessages[randi_range(1, len(deathmessages)-1)]
	tween.tween_property($Label2, "modulate:a", 1, 1)
	tween.tween_property($Button, "modulate:a", 1, 0.5)
	tween.tween_property($Button6, "modulate:a", 1, 0.5)
	tween.tween_property($Button5, "modulate:a", 1, 0.5)
	tween.tween_property($Label5, "modulate:a", 1, 0.5)
	await tween.finished
	$Button.disabled = false
	$Button5.disabled = false
	$Button6.disabled = false

func _on_button_pressed() -> void:
	$Button.disabled = true
	$Button5.disabled = true
	$Button6.disabled = true
	$Label2.visible = false
	$Button.visible = false
	$Label3.visible = false
	$Button5.visible = false
	$Button6.visible = false
	$Label5.visible = false
	$Label2.modulate.a = 0
	$Label5.modulate.a = 0
	$Button.modulate.a = 0
	$Button5.modulate.a = 0
	$Button6.modulate.a = 0
	$TextureRect.visible = true
	$Label.visible = true
	main.startgame()
	


func _on_start_game_pressed() -> void:
	main.startgame()
	main.get_node("Character").visible = true
	$mainmenu.visible = false
	$TextureRect.visible = true
	$Label.visible = true


func _on_button_4_pressed() -> void:
	get_tree().quit()

func _on_button_3_pressed() -> void:
	$mainmenu/MouseLeftClick.visible = false
	$mainmenu/MouseRightClick.visible = false
	$mainmenu/SmallBubble.visible = false
	$mainmenu/SmallBubbleWDebris.visible = false
	$mainmenu/LonesomeNet.visible = false
	$mainmenu/Label2.visible = false
	$mainmenu/Label.visible = false
	$mainmenu/Label3.visible = false
	$mainmenu/ColorRect.visible = true
	$mainmenu/ColorRect2.visible = true
	$mainmenu/Label4.visible = true
	$mainmenu/Label5.visible = true

func _on_button_6_pressed() -> void:
	$Label2.visible = false
	$Button.visible = false
	$Label3.visible = false
	$Button5.visible = false
	$Button6.visible = false
	$Label5.visible = false
	$Label2.modulate.a = 0
	$Label5.modulate.a = 0
	$Button.modulate.a = 0
	$Button5.modulate.a = 0
	$Button6.modulate.a = 0
	get_tree().reload_current_scene()
